﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace JJYYCore.CSV
{
    public abstract class GameDataBase
    {
        protected readonly int safeRowCount;

        protected abstract void SetByCSV(string[] csvLine);

        public static List<T> GetDatasByCSVList<T>(List<string[]> csvDataList) where T:GameDataBase,new()
        {
            List<T> datas = new List<T>();
            if (csvDataList.Count == 0)
                Debug.LogError("加载的CSV资源长度为0!资源类型为:"+typeof(T));

            for (int i = 0; i < csvDataList.Count; i++)
            {
                T line = new T();

                if (csvDataList[i].Length != line.safeRowCount)
                {
                    Debug.LogError("识别到与表格长度不匹配的行！行数为："+(i+1));
                    Debug.LogError("安全长度为："+line.safeRowCount+"，但该列的长度为："+csvDataList[i].Length);
                    continue;
                }

                line.SetByCSV(csvDataList[i]);
                datas.Add(line);
            }

            return datas;
        }

        protected GameDataBase(int safeRowCount)
        {
            this.safeRowCount = safeRowCount;
        }
    }
}